package com.javaex.book.services.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.javaex.book.mapper.BookMapper;
import com.javaex.book.mapper.OrderItemMapper;
import com.javaex.book.mapper.OrderMapper;
import com.javaex.book.pojo.Book;
import com.javaex.book.pojo.Cart;
import com.javaex.book.pojo.CartItem;
import com.javaex.book.pojo.Order;
import com.javaex.book.pojo.OrderItem;
import com.javaex.book.services.OrderService;
import com.javaex.book.utils.Constants;
import com.javaex.book.utils.Pagination;
import com.javaex.book.utils.ReturnResult;

@Service
public class OrderServiceImpl implements OrderService {
	
	private final OrderMapper orderMapper;
	private final OrderItemMapper orderItemMapper;
	private final BookMapper bookMapper;
	
	@Autowired
	public OrderServiceImpl(OrderMapper orderMapper, OrderItemMapper orderItemMapper, BookMapper bookMapper) {
		this.orderMapper = orderMapper;
		this.orderItemMapper = orderItemMapper;
		this.bookMapper = bookMapper;
	}

	@Transactional
	@Override
	public ReturnResult createOrder(Cart cart, Integer userId) {
		String orderId = System.currentTimeMillis() + "" + userId;
		// 保存订单
		Order order = new Order(orderId, userId, cart.getTotalPrice());
		orderMapper.insertOrder(order);
		for (CartItem item : cart.getItems()) {
			// 保存订单项
			OrderItem orderItem = new OrderItem(item.getName(), item.getCount(), item.getPrice(), orderId, item.getTotalPrice());
			orderItemMapper.insertOrderItem(orderItem);
			
			// 修改商品的库存和销量
			Book book = bookMapper.selectBookById(item.getId());
			book.setSales(book.getSales() + item.getCount());
			book.setStock(book.getStock() - item.getCount());
			bookMapper.updateBook(book);
		}
		return ReturnResult.success("订单结算成功", orderId);
	}

	@Override
	public Pagination getOrderList(Integer userId, Integer pageNo, Integer pageSize) {
		Integer count = orderMapper.countOrder(userId);
		Pagination pagination = new Pagination(pageNo, pageSize, count);
		List<Order> orderList = orderMapper.selectOrderList(userId, pagination.getStart(), pageSize);
		pagination.setData(orderList);
		return pagination; 
	}

	@Override
	public List<OrderItem> getOrderDetail(String orderId) {
		return orderItemMapper.selectOrderItemListByOrderId(orderId);
	}

	@Override
	public ReturnResult receiverOrder(String orderId) {
		// 根据订单id查询订单。只有当订单状态为已发货时，才能签收
		Order order = orderMapper.selectOrderByOrderId(orderId);
		if (order.getStatus() == Constants.ORDER_NOT_SEND || order.getStatus() == Constants.ORDER_RECEIVED) {
			return ReturnResult.fail("订单未发货或已签收");
		}
		// 修改订单状态，签收订单
		Integer rows = orderMapper.updateOrderStatus(orderId, Constants.ORDER_RECEIVED);
		if (rows == null || rows == 0) {
			return ReturnResult.fail("订单签收失败");
		}
		return ReturnResult.success("订单签收成功").setData(Constants.ORDER_RECEIVED);
	}

	@Override
	public ReturnResult sendOrder(String orderId) {
		// 根据订单id查询订单。只有当订单状态为未发货时，才能发货
		Order order = orderMapper.selectOrderByOrderId(orderId);
		if (order.getStatus() == Constants.ORDER_SEND || order.getStatus() == Constants.ORDER_RECEIVED) {
			return ReturnResult.fail("订单已发货或已签收");
		}
		// 修改订单状态，签收订单
		Integer rows = orderMapper.updateOrderStatus(orderId, Constants.ORDER_SEND);
		if (rows == null || rows == 0) {
			return ReturnResult.fail("订单发货失败");
		}
		return ReturnResult.success("订单发货成功").setData(Constants.ORDER_SEND);
	}

	@Transactional
	@Override
	public ReturnResult deleteOrder(String orderId) {
		// 根据订单id查询订单。只有当订单状态为已签收时，才能删除
		Order order = orderMapper.selectOrderByOrderId(orderId);
		if (order.getStatus() != Constants.ORDER_RECEIVED) {
			return ReturnResult.fail("订单还未签收");
		}
		
		// 删除订单项
		Integer rows = orderItemMapper.deleteOrderItem(orderId);
		if (rows == null || rows == 0) {
			return ReturnResult.fail("订单删除失败");
		}

		// 删除订单
		rows = orderMapper.deleteOrder(orderId);
		if (rows == null || rows == 0) {
			return ReturnResult.fail("订单删除失败");
		}
		return ReturnResult.success("订单删除成功");
	}
}
